package com.softWang.system.service;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.softWang.system.controller.vo.systemArea.SystemAreaAddParam;
import com.softWang.system.controller.vo.systemArea.SystemAreaEditParam;
import com.softWang.system.controller.vo.systemArea.SystemAreaQueryAllParam;
import com.softWang.system.controller.vo.systemArea.SystemAreaQueryAllResult;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.List;

import com.softWang.common.base.BaseServiceMy;
import com.softWang.system.po.SystemArea;
import com.softWang.system.mapper.SystemAreaMapper;

/**
 * <p>
 * 系统管理--行政区划代码表 服务实现类
 * </p>
 *
 * @author wang
 * @since 2025-07-21
 */
@Service
public class SystemAreaService extends BaseServiceMy<SystemArea> {

     @Resource
     private SystemAreaMapper systemAreaMapper;

     /**
     * 分页条件查询
     * @param param
     * @return
     */
     public List<SystemAreaQueryAllResult> queryall(SystemAreaQueryAllParam param){
         param.startPage();
         List<SystemAreaQueryAllResult> results=systemAreaMapper.queryall(param);
         return results;
     }

      /**
      * 插入
      * @param param 数据信息
      * @return id
      */
      @Transactional
      public Long add(SystemAreaAddParam param) {

          SystemArea entity = new SystemArea();
          BeanUtils.copyProperties(param, entity);

          systemAreaMapper.insert(entity);
          return entity.getCode();
      }

      /**
      * 修改
      * @param param  数据信息
      * @return
      */
      @Transactional
      public Long edit(SystemAreaEditParam param) {

         SystemArea entity = new SystemArea();
         BeanUtils.copyProperties(param, entity);

         systemAreaMapper.updateById(entity);
         return entity.getCode();
      }

      /**
      * 获取详情
      * @param id id
      * @return 详情信息
      */
      public SystemArea getById(Long id) {
           return systemAreaMapper.selectById(id);
      }

      /**
      * 删除
      * @param ids id列表
      * @return void
      */
      @Transactional
      public void del(List<Long> ids) {
          systemAreaMapper.deleteBatchIds(ids);
      }

    /**
     * 获取树列表
     * @param areaId 异步加载时需要传父id
     * @return 树列表
     */
    public List<SystemArea> getAreaTree(Long areaId) {
        List<SystemArea> areas = systemAreaMapper.selectList(Wrappers.lambdaQuery(SystemArea.class)
                .eq(areaId!=null,SystemArea::getPid, areaId)
                .orderByAsc(SystemArea::getCode));
        return areas;
    }
}
